﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Fantacid303.ProcessAffinityControl.Manager.Extensions;
using System.Windows.Input;
using Fantacid303.ProcessAffinityControl.Manager.Properties;

namespace Fantacid303.ProcessAffinityControl.Manager.MainWindowViewModels
{

	partial class ConfigurationEntryViewModel
    {
		private class MySetActiveCommand : MyCommandBase<ConfigurationEntryViewModel>
		{
			public MySetActiveCommand(ConfigurationEntryViewModel model, bool autoSave) : base(model)
			{
				this.AutoSave = autoSave;
			}

			public bool AutoSave
			{ get; private set; }

			public override bool CanExecute(object parameter)
			{
				return !this.Model.IsActive;
			}
			public override void Execute(object parameter)
			{
				var entries = new List<ConfigurationEntryViewModel>(this.Model.ConfigurationViewModel.ConfigurationEntries);
				entries.ForEach(a => a.IsActive= (a==this.Model) );

				// make the TreeView to load all entries again in order to make sure, 
				// that the data template selectors get re-evaluated
				this.Model.ConfigurationViewModel.ConfigurationEntries.Clear();
				this.Model.ConfigurationViewModel.ConfigurationEntries.AddRange(entries);

				if (this.AutoSave)
				{
					this.Model.ConfigurationViewModel.SaveCommand.Execute(null);
					this.Model.ConfigurationViewModel.StatusMessage = string.Format("The scenario '{0}' is active and the configuration has been saved.", this.Model.Name);
				}
				else
				{
					this.Model.ConfigurationViewModel.StatusMessage = Messages.ConfigNotSaved;
				}
			}
		}

        private class MyAddRuleCommand : MyCommandBase<ConfigurationEntryViewModel>
        {
			public MyAddRuleCommand(ConfigurationEntryViewModel model) : base(model)
            {
            }

            public override bool CanExecute(object parameter)
            {
                return true;
            }
            public override void Execute(object parameter)
            {
				var rule = new ConfigurationRuleViewModel(this.Model) { AffinityType = Common.ProcessorAffinityType.AllCores, ProcessName="*" };
				this.Model.Rules.Add(rule);

				this.Model.ConfigurationViewModel.StatusMessage = Messages.ConfigNotSaved;
            }
        }

        private class MyDeleteCommand : MyCommandBase<ConfigurationEntryViewModel>
        {
			public MyDeleteCommand(ConfigurationEntryViewModel model) : base(model)
            {}

            public override bool CanExecute(object parameter)
            {
                return true;
            }

            public override void Execute(object parameter)
            {
				this.Model.ConfigurationViewModel.ConfigurationEntries.RemoveAll(a => a == this.Model);
				this.Model.ConfigurationViewModel.StatusMessage = Messages.ConfigNotSaved;
            }
        }

		/// <summary>
		/// Gets the command which deletes (remove) this command from the configuration entries collection
		/// </summary>
		/// <value>The delete command.</value>
		public ICommand DeleteCommand { get; private set; }

		/// <summary>
		/// Gets the set command which sets this entry active.
		/// </summary>
		/// <value>The set active command.</value>
		public ICommand SetActiveCommand { get; private set; }

		/// <summary>
		/// Gets the set command which sets this entry active.
		/// </summary>
		/// <value>The set active command.</value>
		public ICommand SetActiveAndSaveCommand { get; private set; }


		/// <summary>
		/// Gets the command which adds a new, default rule to the entry.
		/// </summary>
		/// <value>The add rule command.</value>
		public ICommand AddRuleCommand { get; private set; }
    }
}
